package com.itheima.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/user")
public class UserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws  IOException {
        String method = req.getParameter("method");
        if("findAll".equals(method)){
            findAll(req,resp);
        }
        if("add".equals(method)){
            add(req,resp);
        }
        if("delete".equals(method)){
            delete(req,resp);
        }
        if("findById".equals(method)){
            findById(req,resp);
        }
        if("update".equals(method)) {
            update(req,resp);
        }
    }
    //查询所有
    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws  IOException {
        UserService userService=new UserServiceImpl();
        System.out.println("调试窗口findAll....");
        List<User> list=userService.findAll();
        //将list转换成json数据
        ObjectMapper obj=new ObjectMapper();
        String json = obj.writeValueAsString(list);

        resp.getWriter().write(json);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws  IOException {
        BufferedReader reader = req.getReader();
        Gson gson = new Gson();
        User user = gson.fromJson(reader, User.class);
        UserService userService=new UserServiceImpl();
        System.out.println("调试窗口add....");
        userService.add(user);
        resp.getWriter().write("true");

    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws  IOException {
        BufferedReader reader = req.getReader();
        Gson gson = new Gson();
        Integer[] ids = gson.fromJson(reader, Integer[].class);
        UserService userService=new UserServiceImpl();
        System.out.println("调试窗口delete....");
        userService.delete(ids);
       /* resp.getWriter().write("true");*/

    }
    private void findById(HttpServletRequest req, HttpServletResponse resp) throws  IOException {
        String id = req.getParameter("id");
        int uid = Integer.parseInt(id);
        UserService service = new UserServiceImpl();
        System.out.println("调试窗口findById....");
        User user= service.findById(uid);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(user);
        resp.getWriter().write(json);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws  IOException {
        BufferedReader reader = req.getReader();
        Gson gson = new Gson();
        User user = gson.fromJson(reader, User.class);
        UserService userService=new UserServiceImpl();
        System.out.println("调试窗口update....");
        userService.update(user);

    }

}
